package com.huikeportal.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huikeportal.controller.common.Lang;
import com.huikeportal.model.ChangePwd;
import com.huikeportal.model.Language;
import com.huikeportal.model.MessageCommon;
import com.huikeportal.model.UserInfo;
import com.huikeportal.model.UserManagerInfo;
import com.huikeportal.service.ChangePwdService;
import com.huikeportal.service.LoginService;

@Controller
@Scope("prototype")
public class ChangePwdController {

	@Autowired
	private HttpSession session;

	@Autowired
	private LoginService loginService;

	@Autowired
	public ChangePwdService changePwdService;

	@InitBinder("changePwd")
	public void initBinder(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("changePwd.");
	}

	// 页面载入
	@RequestMapping("/changePwd")
	public String changePassword(ModelMap model) throws Exception {
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		String menuHtml = loginService.getMenuHtml(userInfo, "changePwd");
		model.addAttribute("menuHtml", menuHtml);// 左侧菜单html

		Language lang = Lang.getInfo("zh_cn");
		model.addAttribute("lang", lang);
		return "ChangePwd";
	}

	// // 修改密码
	// @RequestMapping( "/changeIt")
	// public String changeIt(ChangePwd changePwd, ModelMap model) throws
	// Exception {
	// UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
	// String menuHtml = loginService.getMenuHtml(userInfo, "changePwd");
	// model.addAttribute("menuHtml", menuHtml);// 左侧菜单html
	// userInfo = loginService.getUserInfo(userInfo);
	// String userid=userInfo.getUserId();
	// changePwd.setUserId(userid);
	// MessageCommon messcomm = changePwdService.updatePwd(changePwd);
	// model.addAttribute("message", messcomm.getMessage());
	//		
	// Language lang = Lang.getInfo("zh_cn");
	// model.addAttribute("lang",lang);
	// return "ChangePwd";
	// }
	// 修改密码
	@RequestMapping("/changeIt")
	@ResponseBody
	public Map<String, Object> changeIt(String oldPwd, String newPwd,
			String rePwd, ModelMap model) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		ChangePwd changePwd = new ChangePwd();
		changePwd.setOldPassword(oldPwd);
		changePwd.setNewPassword(newPwd);
		changePwd.setNewPasswordAgain(rePwd);
		UserInfo userInfo = (UserInfo) session.getAttribute("sessionUserName");
		userInfo = loginService.getUserInfo(userInfo);
		changePwd.setUserId(userInfo.getUserId());
		MessageCommon messcomm = changePwdService.updatePwd(changePwd);
		map.put("flag", messcomm.isFlag());
		map.put("message", messcomm.getMessage());
		return map;
	}
}
